﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Common.Class;
using ContractManager.Entity;
using Speed.Data;
namespace ContractManager.BLL.Business
{
    public static class ClsSourceData
    {
        public static List<SourceData> SelectAll()
        {
            using (var db = Sys.NewDb())
            {

                return db.Select<SourceData>();
            }
            
        }


        public static DataTable GetControlDataBySourceId(int? sourceId, string where)
        {
            using (var db = Sys.NewDb())
            {

                var item = SelectByPk(Convert.ToInt32(sourceId));
                string sql = item.SourceDataValue;
                if (item.SourceDataType == 1)
                {
                    if (String.IsNullOrEmpty(where))
                    {
                        where = String.Format(" WHERE EmpId={0}", ClsVariable.EmployeeId);
                    }
                    else
                    {
                        where = where + String.Format(" AND EmpId={0}", ClsVariable.EmployeeId);
                    }
                }
                if (item.SourceDataType == 2)
                {
                    if (String.IsNullOrEmpty(where))
                    {
                        where = String.Format(" WHERE MbID={0}", ClsVariable.MemberBranchId);
                    }
                    else
                    {
                        where = where + String.Format(" AND MbID={0}", ClsVariable.MemberBranchId);
                    }
                }
                if (!String.IsNullOrEmpty(where)) sql = sql + " " + where;
                return db.ExecuteDataTable(sql);
            }

        }

        public static DataTable GetGridDataBySourceId(string sql, string where)
        {
            using (var db = Sys.NewDb())
            {



                return db.ExecuteDataTable(sql);
            }

        }

        public static void Insert(SourceData capital)
        {
            using (var db = Sys.NewDb())
            {
                db.Insert<SourceData>(capital, EnumSaveMode.Requery);
            }
           
        }

        public static void Update(SourceData capital)
        {
            using (var db = Sys.NewDb())
            {
                db.Update<SourceData>(capital);
            }
            
            
        }

        public static SourceData SelectByPk(Int32 capId)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<SourceData>(string.Format("SourceDataId={0}", capId));
            }
            
        }


       
        public static int DeleteByPk(Int32 capId)
        {
            using (var db = Sys.NewDb())
            {
                return db.Delete<SourceData>(string.Format("SourceDataId={0}", capId));
            }
            
        }
    }
}